package com.google.android.material.datepicker;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import b.f.l.s;
import com.google.android.material.button.MaterialButton;
import java.util.Calendar;
import java.util.Iterator;

public final class h<S> extends p<S> {
    static final Object m = "MONTHS_VIEW_GROUP_TAG";
    static final Object n = "NAVIGATION_PREV_TAG";
    static final Object o = "NAVIGATION_NEXT_TAG";
    static final Object p = "SELECTOR_TOGGLE_TAG";

    /* renamed from: c  reason: collision with root package name */
    private int f2236c;
    /* access modifiers changed from: private */

    /* renamed from: d  reason: collision with root package name */
    public d<S> f2237d;
    /* access modifiers changed from: private */
    public a e;
    /* access modifiers changed from: private */
    public l f;
    private k g;
    /* access modifiers changed from: private */
    public c h;
    /* access modifiers changed from: private */
    public RecyclerView i;
    /* access modifiers changed from: private */
    public RecyclerView j;
    private View k;
    /* access modifiers changed from: private */
    public View l;

    class a implements Runnable {

        /* renamed from: b  reason: collision with root package name */
        final /* synthetic */ int f2238b;

        a(int i) {
            this.f2238b = i;
        }

        public void run() {
            h.this.j.smoothScrollToPosition(this.f2238b);
        }
    }

    class b extends b.f.l.a {
        b(h hVar) {
        }

        public void g(View view, b.f.l.b0.c cVar) {
            super.g(view, cVar);
            cVar.Z((Object) null);
        }
    }

    class c extends q {
        final /* synthetic */ int I;

        /* JADX INFO: super call moved to the top of the method (can break code semantics) */
        c(Context context, int i, boolean z, int i2) {
            super(context, i, z);
            this.I = i2;
        }

        /* access modifiers changed from: protected */
        public void M1(RecyclerView.a0 a0Var, int[] iArr) {
            if (this.I == 0) {
                iArr[0] = h.this.j.getWidth();
                iArr[1] = h.this.j.getWidth();
                return;
            }
            iArr[0] = h.this.j.getHeight();
            iArr[1] = h.this.j.getHeight();
        }
    }

    class d implements l {
        d() {
        }

        public void a(long j) {
            if (h.this.e.n().e(j)) {
                h.this.f2237d.g(j);
                Iterator it = h.this.f2273b.iterator();
                while (it.hasNext()) {
                    ((o) it.next()).a(h.this.f2237d.a());
                }
                h.this.j.getAdapter().l();
                if (h.this.i != null) {
                    h.this.i.getAdapter().l();
                }
            }
        }
    }

    class e extends RecyclerView.n {

        /* renamed from: a  reason: collision with root package name */
        private final Calendar f2241a = r.k();

        /* renamed from: b  reason: collision with root package name */
        private final Calendar f2242b = r.k();

        e() {
        }

        public void g(Canvas canvas, RecyclerView recyclerView, RecyclerView.a0 a0Var) {
            if ((recyclerView.getAdapter() instanceof s) && (recyclerView.getLayoutManager() instanceof GridLayoutManager)) {
                s sVar = (s) recyclerView.getAdapter();
                GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
                for (b.f.k.d next : h.this.f2237d.d()) {
                    F f = next.f1297a;
                    if (!(f == null || next.f1298b == null)) {
                        this.f2241a.setTimeInMillis(((Long) f).longValue());
                        this.f2242b.setTimeInMillis(((Long) next.f1298b).longValue());
                        int F = sVar.F(this.f2241a.get(1));
                        int F2 = sVar.F(this.f2242b.get(1));
                        View C = gridLayoutManager.C(F);
                        View C2 = gridLayoutManager.C(F2);
                        int X2 = F / gridLayoutManager.X2();
                        int X22 = F2 / gridLayoutManager.X2();
                        int i = X2;
                        while (i <= X22) {
                            View C3 = gridLayoutManager.C(gridLayoutManager.X2() * i);
                            if (C3 != null) {
                                canvas.drawRect((float) (i == X2 ? C.getLeft() + (C.getWidth() / 2) : 0), (float) (C3.getTop() + h.this.h.f2231d.c()), (float) (i == X22 ? C2.getLeft() + (C2.getWidth() / 2) : recyclerView.getWidth()), (float) (C3.getBottom() - h.this.h.f2231d.b()), h.this.h.h);
                            }
                            i++;
                        }
                    }
                }
            }
        }
    }

    class f extends b.f.l.a {
        f() {
        }

        public void g(View view, b.f.l.b0.c cVar) {
            h hVar;
            int i;
            super.g(view, cVar);
            if (h.this.l.getVisibility() == 0) {
                hVar = h.this;
                i = c.b.a.b.j.mtrl_picker_toggle_to_year_selection;
            } else {
                hVar = h.this;
                i = c.b.a.b.j.mtrl_picker_toggle_to_day_selection;
            }
            cVar.i0(hVar.getString(i));
        }
    }

    class g extends RecyclerView.t {

        /* renamed from: a  reason: collision with root package name */
        final /* synthetic */ n f2245a;

        /* renamed from: b  reason: collision with root package name */
        final /* synthetic */ MaterialButton f2246b;

        g(n nVar, MaterialButton materialButton) {
            this.f2245a = nVar;
            this.f2246b = materialButton;
        }

        public void a(RecyclerView recyclerView, int i) {
            if (i == 0) {
                CharSequence text = this.f2246b.getText();
                if (Build.VERSION.SDK_INT >= 16) {
                    recyclerView.announceForAccessibility(text);
                } else {
                    recyclerView.sendAccessibilityEvent(2048);
                }
            }
        }

        public void b(RecyclerView recyclerView, int i, int i2) {
            LinearLayoutManager r = h.this.r();
            int Z1 = i < 0 ? r.Z1() : r.c2();
            l unused = h.this.f = this.f2245a.E(Z1);
            this.f2246b.setText(this.f2245a.F(Z1));
        }
    }

    /* renamed from: com.google.android.material.datepicker.h$h  reason: collision with other inner class name */
    class C0099h implements View.OnClickListener {
        C0099h() {
        }

        public void onClick(View view) {
            h.this.w();
        }
    }

    class i implements View.OnClickListener {

        /* renamed from: b  reason: collision with root package name */
        final /* synthetic */ n f2249b;

        i(n nVar) {
            this.f2249b = nVar;
        }

        public void onClick(View view) {
            int Z1 = h.this.r().Z1() + 1;
            if (Z1 < h.this.j.getAdapter().g()) {
                h.this.u(this.f2249b.E(Z1));
            }
        }
    }

    class j implements View.OnClickListener {

        /* renamed from: b  reason: collision with root package name */
        final /* synthetic */ n f2251b;

        j(n nVar) {
            this.f2251b = nVar;
        }

        public void onClick(View view) {
            int c2 = h.this.r().c2() - 1;
            if (c2 >= 0) {
                h.this.u(this.f2251b.E(c2));
            }
        }
    }

    enum k {
        DAY,
        YEAR
    }

    interface l {
        void a(long j);
    }

    private void k(View view, n nVar) {
        MaterialButton materialButton = (MaterialButton) view.findViewById(c.b.a.b.f.month_navigation_fragment_toggle);
        materialButton.setTag(p);
        s.e0(materialButton, new f());
        MaterialButton materialButton2 = (MaterialButton) view.findViewById(c.b.a.b.f.month_navigation_previous);
        materialButton2.setTag(n);
        MaterialButton materialButton3 = (MaterialButton) view.findViewById(c.b.a.b.f.month_navigation_next);
        materialButton3.setTag(o);
        this.k = view.findViewById(c.b.a.b.f.mtrl_calendar_year_selector_frame);
        this.l = view.findViewById(c.b.a.b.f.mtrl_calendar_day_selector_frame);
        v(k.DAY);
        materialButton.setText(this.f.o());
        this.j.addOnScrollListener(new g(nVar, materialButton));
        materialButton.setOnClickListener(new C0099h());
        materialButton3.setOnClickListener(new i(nVar));
        materialButton2.setOnClickListener(new j(nVar));
    }

    private RecyclerView.n l() {
        return new e();
    }

    static int q(Context context) {
        return context.getResources().getDimensionPixelSize(c.b.a.b.d.mtrl_calendar_day_height);
    }

    static <T> h<T> s(d<T> dVar, int i2, a aVar) {
        h<T> hVar = new h<>();
        Bundle bundle = new Bundle();
        bundle.putInt("THEME_RES_ID_KEY", i2);
        bundle.putParcelable("GRID_SELECTOR_KEY", dVar);
        bundle.putParcelable("CALENDAR_CONSTRAINTS_KEY", aVar);
        bundle.putParcelable("CURRENT_MONTH_KEY", aVar.q());
        hVar.setArguments(bundle);
        return hVar;
    }

    private void t(int i2) {
        this.j.post(new a(i2));
    }

    /* access modifiers changed from: package-private */
    public a m() {
        return this.e;
    }

    /* access modifiers changed from: package-private */
    public c n() {
        return this.h;
    }

    /* access modifiers changed from: package-private */
    public l o() {
        return this.f;
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (bundle == null) {
            bundle = getArguments();
        }
        this.f2236c = bundle.getInt("THEME_RES_ID_KEY");
        this.f2237d = (d) bundle.getParcelable("GRID_SELECTOR_KEY");
        this.e = (a) bundle.getParcelable("CALENDAR_CONSTRAINTS_KEY");
        this.f = (l) bundle.getParcelable("CURRENT_MONTH_KEY");
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        int i2;
        int i3;
        ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getContext(), this.f2236c);
        this.h = new c(contextThemeWrapper);
        LayoutInflater cloneInContext = layoutInflater.cloneInContext(contextThemeWrapper);
        l r = this.e.r();
        if (i.A(contextThemeWrapper)) {
            i3 = c.b.a.b.h.mtrl_calendar_vertical;
            i2 = 1;
        } else {
            i3 = c.b.a.b.h.mtrl_calendar_horizontal;
            i2 = 0;
        }
        View inflate = cloneInContext.inflate(i3, viewGroup, false);
        GridView gridView = (GridView) inflate.findViewById(c.b.a.b.f.mtrl_calendar_days_of_week);
        s.e0(gridView, new b(this));
        gridView.setAdapter(new g());
        gridView.setNumColumns(r.f);
        gridView.setEnabled(false);
        this.j = (RecyclerView) inflate.findViewById(c.b.a.b.f.mtrl_calendar_months);
        this.j.setLayoutManager(new c(getContext(), i2, false, i2));
        this.j.setTag(m);
        n nVar = new n(contextThemeWrapper, this.f2237d, this.e, new d());
        this.j.setAdapter(nVar);
        int integer = contextThemeWrapper.getResources().getInteger(c.b.a.b.g.mtrl_calendar_year_selector_span);
        RecyclerView recyclerView = (RecyclerView) inflate.findViewById(c.b.a.b.f.mtrl_calendar_year_selector_frame);
        this.i = recyclerView;
        if (recyclerView != null) {
            recyclerView.setHasFixedSize(true);
            this.i.setLayoutManager(new GridLayoutManager((Context) contextThemeWrapper, integer, 1, false));
            this.i.setAdapter(new s(this));
            this.i.addItemDecoration(l());
        }
        if (inflate.findViewById(c.b.a.b.f.month_navigation_fragment_toggle) != null) {
            k(inflate, nVar);
        }
        if (!i.A(contextThemeWrapper)) {
            new androidx.recyclerview.widget.k().b(this.j);
        }
        this.j.scrollToPosition(nVar.G(this.f));
        return inflate;
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putInt("THEME_RES_ID_KEY", this.f2236c);
        bundle.putParcelable("GRID_SELECTOR_KEY", this.f2237d);
        bundle.putParcelable("CALENDAR_CONSTRAINTS_KEY", this.e);
        bundle.putParcelable("CURRENT_MONTH_KEY", this.f);
    }

    public d<S> p() {
        return this.f2237d;
    }

    /* access modifiers changed from: package-private */
    public LinearLayoutManager r() {
        return (LinearLayoutManager) this.j.getLayoutManager();
    }

    /* access modifiers changed from: package-private */
    public void u(l lVar) {
        RecyclerView recyclerView;
        int i2;
        n nVar = (n) this.j.getAdapter();
        int G = nVar.G(lVar);
        int G2 = G - nVar.G(this.f);
        boolean z = true;
        boolean z2 = Math.abs(G2) > 3;
        if (G2 <= 0) {
            z = false;
        }
        this.f = lVar;
        if (!z2 || !z) {
            if (z2) {
                recyclerView = this.j;
                i2 = G + 3;
            }
            t(G);
        }
        recyclerView = this.j;
        i2 = G - 3;
        recyclerView.scrollToPosition(i2);
        t(G);
    }

    /* access modifiers changed from: package-private */
    public void v(k kVar) {
        this.g = kVar;
        if (kVar == k.YEAR) {
            this.i.getLayoutManager().x1(((s) this.i.getAdapter()).F(this.f.e));
            this.k.setVisibility(0);
            this.l.setVisibility(8);
        } else if (kVar == k.DAY) {
            this.k.setVisibility(8);
            this.l.setVisibility(0);
            u(this.f);
        }
    }

    /* access modifiers changed from: package-private */
    public void w() {
        k kVar;
        k kVar2 = this.g;
        if (kVar2 == k.YEAR) {
            kVar = k.DAY;
        } else if (kVar2 == k.DAY) {
            kVar = k.YEAR;
        } else {
            return;
        }
        v(kVar);
    }
}
